﻿/*************************************************************
Base Common Js.
Create By Ming.ma
2014-08-06 V2.0
*************************************************************/


//----Start Base Function
function $$$(s) {
    return typeof s == 'string' ? document.getElementById(s) : s;
}
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Namespace = new Object();
Namespace.register = function (fullNS) {
    var nsArray = fullNS.split('.');
    var sEval = "";
    var sNS = "";
    for (var i = 0; i < nsArray.length; i++) {
        if (i != 0) sNS += ".";
        sNS += nsArray[i];
        sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS + " = new Object();"
    }
    if (sEval != "") eval(sEval);
}

Namespace.register('$string');
Namespace.register('$object');

//格式化字符串 模仿String.Format
$string.format = function () {
    var sRet = arguments.length > 0 ? arguments[0] : '';
    if (arguments.length > 0) {
        var matchList = arguments[0].match(/{\d+}/g);
        for (var i = 0; i < matchList.length; i++) {
            sRet = sRet.replace(new RegExp(matchList[i].replace('{', '\\{').replace('}', '\\}'), 'g'), arguments[parseInt(matchList[i].match(/\d+/g)) + 1]);
        }
    }
    return sRet;
};
//去除字符串俩头空格
$string.trim = function (o) { return o.replace(/(^\s+)|(\s+$)/, '') };
//判断字符串是否为空
$string.isEmpty = function (o) { return null == o || undefined == o || o == ''; };
//判断对象是否为空
$object.isNull = function (o) { return null == o || undefined == o; };
//判断是否是数组
$object.isArray = function (o) { return Object.prototype.toString.call(o) === '[object Array]'; };
//拷贝对象(支持数组)
$object.copy = function (o) { var _o = {}; if (null == o || typeof (o) != 'object') { _o = o } else { for (var i in o) { _o[i] = arguments.callee(o[i]) } } return _o; };
//----End Base Function

//----Start Event
Namespace.register('$evnet');
$evnet.handleHash = {};
$evnet.bind = (function () {
    if (window.addEventListener) {
        return function (el, type, fn, capture) {
            el.addEventListener(type, function () {
                fn(el);
                $$$handleHash[type] = this.handleHash[type] || [];
                $$$handleHash[type].push(arguments.callee);
            }, capture);
        };
    } else if (window.attachEvent) {
        return function (el, type, fn, capture) {
            el.attachEvent("on" + type, function () {
                fn(el);
                $$$handleHash[type] = this.handleHash[type] || [];
                $$$handleHash[type].push(arguments.callee);
            });
        };
    }
})();
$evnet.unbind = (function () {
    if (window.addEventListener) {
        return function (el, type) {
            if ($$$handleHash[type]) {
                var i = 0, len = $$$handleHash[type].length;
                for (i; i < len; i += 1) {
                    el.removeEventListener(type, $$$handleHash[type][i]);
                }
            };
        };
    } else if (window.attachEvent) {
        return function (el, type) {
            if ($$$handleHash[type]) {
                var i = 0, len = $$$handleHash[type].length;
                for (i; i < len; i += 1) {
                    el.detachEvent("on" + type, $$$handleHash[type][i]);
                }
            };
        };
    }
})();
//----End Event 

//----Start Dom
Namespace.register('$dom');
//Create Element
$dom.CE = function (t, a) {
    var _t = document.createElement(t); if (a) { for (var _a in a) { _t.setAttribute(_a, a[_a]); } } return _t;
}
$dom.GT = function (n) {
    return document.getElementsByTagName(n);
}
//Load JavaScript File.
//this function load need any time .
$dom.loadJs = function (s, f) {
    var script = this.CE('script', { 'language': 'javascript', 'type': 'text/javascript', 'src': s });
    typeof (f) == 'function' ? $evnet.bind(script, 'load', f) : null;
    this.GT('HEAD')[0].appendChild(script);
}
//Get Url Param
$dom.GUP = function (u, n) {
    var r = new RegExp('[\?&]' + n + '=([^&]+)', 'i');
    var m = r.exec(u);
    if (m && m.length > 1)
        return m[1];
    else
        return '';
}
//----End Dom